package com.unhz.taigu.service.impl;

import com.unhz.generator.dao.SysRoleMapper;
import com.unhz.generator.pojo.SysRole;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.IsNumeric;
import com.unhz.generator.utils.Page;
import com.unhz.taigu.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    static final int PAGENUM = 1;
    @Autowired
    private SysRoleMapper sysRoleMapper;


    @Override
    public List<SysRole> findAllRole() {
        return sysRoleMapper.selectAllRole();
    }

    @Override
    public Page<SysRole> findPageRole(String pageNo, Integer pageSize, String keywords) {

        int pageNum = PAGENUM;
        boolean numok = IsNumeric.isNumeric(pageNo);
        if(numok){
            if(pageNo !=null && !"".equals(pageNo)){
                pageNum = Integer.parseInt((pageNo));
            }
        }
        int totalRecords = sysRoleMapper.getTotalRecordsByKeywords("%"+keywords+"%");        //获取记录总条数
        Page page = new Page(pageNum,totalRecords);
        page.setPageSize(pageSize);
        Map<String ,Object> map = new HashMap<String,Object>();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize",page.getPageSize());
        map.put("keywords","%"+keywords+"%");
        //获取总记录数
        List<SysRole> records = sysRoleMapper.findRoleMap(map);
        page.setRecords(records);
        String url = "/sysRoleController/findAllRole?keywords="+keywords;
        page.setUri(url);
        return page;

    }

    @Override
    public int delete(Integer roleId) {
        return sysRoleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public String insertRole(SysRole sysRole) {
        int count = sysRoleMapper.insertSelective(sysRole);
        if(count > 0){
            return "success";
        }
        return "false";
    }

    @Override
    public SysRole selectByRoleId(Integer roleId) {
        return sysRoleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public String updateRole(SysRole sysRole) {

        int count = sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        if(count > 0){
            return "success";
        }
        return "false";
    }
}
